using IoTCenter.Utilities.Extensions;
using System;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;

namespace Ganweisoft.IoTCenter.Module.MediaPlatform.Utilities
{
    public static class StringUtil
    {
        public static bool IsIp(this string ip)
        {
            string pattern = @"^(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$";

            return Regex.IsMatch(ip, pattern);
        }

        public static bool IsUrl(this string url)
        {
            if (url.IsEmpty())
            {
                return false;
            }

            Uri uriResult;
            if (!Uri.TryCreate(url, UriKind.Absolute, out uriResult))
            {
                return false;
            }

            return uriResult.IsAbsoluteUri &&
                (uriResult.Scheme == Uri.UriSchemeHttp ||
                uriResult.Scheme == Uri.UriSchemeHttps ||
                uriResult.Scheme == "rtsps" ||
                uriResult.Scheme == "rtsp" ||
                uriResult.Scheme == "rtmps" ||
                uriResult.Scheme == "rtmp");
        }

        private static readonly string[] _enTextArr = new string[] { "a", "b", "c", "d", "e", "f", "g", "h", "k", "m", "n", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "M", "N", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };

        public static string GenerateRandomKey(int length)
        {
            var sb = new StringBuilder();
            if (length > 0)
            {
                do
                {
                    if (RandomNumberGenerator.GetInt32(0, 2) > 0)
                    {
                        sb.Append(RandomNumberGenerator.GetInt32(2, 10));
                    }
                    else
                    {
                        sb.Append(_enTextArr[RandomNumberGenerator.GetInt32(0, _enTextArr.Length)]);
                    }
                }
                while (--length > 0);
            }
            return sb.ToString();
        }

        public static string GetLocalIpAddress()
        {
            var host = Dns.GetHostEntry(Dns.GetHostName());
            if (host == null || host.AddressList.IsEmpty())
            {
                return string.Empty;
            }

            return host.AddressList
                .Where(x => x.AddressFamily == AddressFamily.InterNetwork)
                .Select(x => x.ToString())
                .FirstOrDefault();
        }

        public static string ToReplace(this string value, string pattern, string replacement = "", RegexOptions options = RegexOptions.IgnoreCase)
        {
            if (value.IsEmpty() || pattern.IsEmpty())
            {
                return value;
            }

            return Regex.Replace(value, pattern, replacement, options);
        }
    }
}
